package com.authine.cloudpivot.ext.service.impl;

import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.entity.order.extendsAttr.OrderCustomerExtendsAttr;
import com.authine.cloudpivot.ext.enums.order.OrderSyncEventTypeEnum;
import com.authine.cloudpivot.ext.model.order.*;
import com.authine.cloudpivot.ext.service.OrderPublishEventService;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.cloudpivot.ext.utils.RegexUtils;
import com.authine.cloudpivot.ext.work.OrderSyncService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: liwei
 * @DateTime: 2021/10/26
 * @Description: TODO
 */
public class OrderPublishEventServiceImpl implements OrderPublishEventService {

    private final OrderSyncService orderSyncService = new OrderSyncService();


    @Override
    public void onApplicationEvent(OrderRemoteSyncEventParam param) {

//        orderPunchoutService.push(event.getParam());

//        同步开关 配置
//        if (!orderSync) {
//            return;
//        }

        this.sync(param);
//    日志同步
    }

    private void sync(OrderRemoteSyncEventParam param) {
        OrderSync orderSync = new OrderSync();
        //如果是代购确认生成订单信息同步，单独进行
        if (OrderSyncEventTypeEnum.ConfirmContract == param.getType()) {
            for (String orderCustomerId : param.getOrderCustomerIds()) {
                orderSync = this.orderCreate(orderCustomerId);
                //todo
                //orderSyncService.sync(param.getType(), orderSync);
            }

            orderSync = this.saveTransport(param.getTraceIds());
            //todo
            //orderSyncService.sync(param.getType(), orderSync);
            return;
        }

        if (OrderSyncEventTypeEnum.SaveTransport == param.getType() && !CollectionUtils.isEmpty(param.getOrderVendorIds())) {
            //先接单
            orderSync = this.orderTaking(param.getOrderVendorIds());
            //todo
            //orderSyncService.sync(param.getType(), orderSync);
        }

        switch (param.getType()) {
            case OrderCreate:
                orderSync = this.orderCreate(param.getOrderCustomerId());
                orderSync.setEvent(OrderSyncEventTypeEnum.OrderCreate.name());
                break;
            case OrderCancel:
                orderSync = this.orderCancel(param.getOrderCustomerIds());
                break;
            case TraceUpdate:
                orderSync = this.traceUpdate(param.getTraceId());
                break;
            case UpdateOrderVendorMemo:
                orderSync = this.updateOrderVendorMemo(param.getOrderVendorId());
                break;
            case UploadReceiveAttachment:
                orderSync = this.uploadReceiveAttachment(param.getOrderCustomerGoodsIds());
                orderSync.setEvent(OrderSyncEventTypeEnum.UploadReceiveAttachment.name());
                break;
            case DelReceiveAttachment:
                orderSync = this.delReceiveAttachment(param.getOrderCustomerGoodsIds());
                break;
            case UpdateOrderGoodsReturns:
                orderSync = this.updateOrderGoodsReturns(param.getOrderGoodsReturnsId());
                break;
            case UpdateTaskVersion:
                orderSync = this.updateTaskVersion(param.getOrderCustomerIds());
                break;
            case SubmitApprove:
                orderSync = this.submitApprove(param.getOrderCustomerIds());
                break;
            case VmiOrder:
                orderSync = this.vmiOrder(param.getOrderCustomerId());
                break;
            case UpdateVendorDeliveryDelay:
                orderSync = this.updateVendorDeliveryDelay(param.getOrderCustomerGoodsIds());
                orderSync.setEvent(OrderSyncEventTypeEnum.UpdateVendorDeliveryDelay.name());
                break;
            case SaveTransport:
                orderSync = this.saveTransport(param.getTraceIds());
                break;
            case ApplyRefund:
                orderSync = this.applyRefund(param.getOrderGoodsReturnsIds());
                break;
            case ApprovalRefund:
                orderSync = this.approvalRefund(param.getOrderGoodsReturnsIds());
                break;
            case RefundFinanceConfirm:
                orderSync = this.refundFinanceConfirm(param.getOrderGoodsReturnsIds());
                break;
            case GoodsReturn:
                orderSync = this.goodsReturn(param.getOrderGoodsReturnsIds(), param.getTraceIds());
                break;
            case ConfirmReturn:
                orderSync = this.confirmReturn(param.getOrderGoodsReturnsIds());
                break;
            case OrderPayment:
                orderSync = this.orderPayment(param.getOrderCustomerIds());
                break;
            case GoodsReceipt:
                orderSync = this.goodsReceipt(param.getOrderGoodsAcceptIds());
                break;
            case OrderTaking:
                orderSync = this.orderTaking(param.getOrderVendorIds());
                break;
            case UpdateCustomerOrder:
                orderSync = this.updateCustomerOrder(param.getOrderCustomerId());
                break;
            case UpdateOrderGoods:
                orderSync = this.updateOrderGoods(param.getOrderCustomerGoodsId());
                break;
            case UpdateOrderGoodsExtendsAttr:
                orderSync = this.updateOrderGoodsExtendsAttr(param.getOrderCustomerGoodsId());
                break;
            case AppendCart:
                orderSync = this.appendCart(param.getCartIds());
                break;
            case RemoveCart:
                orderSync = this.removeCart(param.getCartIds());
                break;
            case UpdateCartNum:
                orderSync = this.updateCartNum(param.getCartId());
                break;
            case UpdateCartVendor:
                orderSync = this.updateCartVendor(param.getGoodsIds());
                break;
            case GoodsReceiptRollback:
                orderSync = this.goodsReceiptRollback(param);
                break;
            case UpdateErpPo:
                orderSync = this.updateErpPo(param.getOrderCustomerIds());
                break;
            case UpdateVendorReceiverDelay:
                orderSync = this.updateVendorReceiverDelay(param.getOrderCustomerGoodsIds());
                orderSync.setEvent(OrderSyncEventTypeEnum.UpdateVendorReceiverDelay.name());
                break;
            case BatchUpdateOrderGoodsStatus:
                orderSync = this.batchUpdateOrderGoodsStatus(param.getOrderCustomerGoodsIds());
                break;
            case UpdateVendorDelayReason:
                orderSync = this.updateVendorDelayReason(param.getOrderVendorIds());
                break;
            case WriteBackNumbers:
                orderSync = this.writeBackNumbers(param.getOrderCustomerGoodsIds());
                break;
            default:
                return;
        }

        orderSyncService.sync(param.getType(), orderSync);
    }

    private OrderSync removeCart(List<String> cartIds) {

        return null;
    }

    private OrderSync appendCart(List<String> cartIds) {

        return null;
    }

    private OrderSync updateOrderGoods(String orderCustomerGoodsId) {

        return null;
    }

    private OrderSync updateOrderGoodsExtendsAttr(String orderCustomerGoodsId) {
        return null;
    }

    private OrderSync updateVendorDeliveryDelay(List<String> orderCustomerGoodsIds) {
        return null;
    }

    private OrderSync vmiOrder(String orderCustomerId) {
        return null;
    }

    private OrderSync goodsReceipt(List<String> orderGoodsAcceptIds) {
        return null;
    }

    private OrderSync orderPayment(List<String> orderCustomerIds) {
        return null;
    }

    private OrderSync confirmReturn(List<String> orderGoodsReturnsIds) {
        return null;
    }

    private OrderSync goodsReturn(List<String> orderGoodsReturnsIds, List<String> traceIds) {
        return null;
    }

    private OrderSync refundFinanceConfirm(List<String> orderGoodsReturnsIds) {
        return null;
    }

    private OrderSync submitApprove(List<String> orderCustomerIds) {
        return null;
    }

    private OrderSync approvalRefund(List<String> orderGoodsReturnsIds) {
        return null;
    }

    private OrderSync updateTaskVersion(List<String> orderCustomerIds) {
        return null;
    }

    private OrderSync applyRefund(List<String> orderGoodsReturnsIds) {
        return null;
    }

    private OrderSync goodsReceiptRollback(OrderRemoteSyncEventParam param) {
        return null;
    }

    private OrderSync updateErpPo(List<String> orderCustomerIds) {
        return null;
    }

    private OrderSync updateVendorReceiverDelay(List<String> orderCustomerGoodsIds) {
        return null;
    }

    private OrderSync updateCartVendor(List<String> goodsIds) {
        return null;
    }

    private OrderSync updateCartNum(String cartId) {
        return null;
    }

    private OrderSync updateCustomerOrder(String orderCustomerId) {
        return null;
    }

    private OrderSync delReceiveAttachment(List<String> orderCustomerGoodsIds) {
        return null;
    }

    private OrderSync uploadReceiveAttachment(List<String> orderCustomerGoodsIds) {


        return null;
    }

    private OrderSync updateOrderVendorMemo(String orderVendorId) {

        OrderVendorVO orderVendorVO = JdbcTemplateUtils.builder(ModelConst.T_ORDER_VENDOR)
                .selectField("*").eq("id", orderVendorId).queryForPOJO(OrderVendorVO.class);
        Assert.notNull(orderVendorVO,"根据供应商订单ID未查询到供应商订单数据");

        //todo

        return null;
    }

    private OrderSync batchUpdateOrderGoodsStatus(List<String> orderCustomerGoodsIds) {
        return null;
    }

    private OrderSync updateVendorDelayReason(List<String> orderVendorIds) {
        return null;
    }

    private OrderSync writeBackNumbers(List<String> orderCustomerGoodsIds) {
        return null;
    }

    private OrderSync updateOrderGoodsReturns(String orderGoodsReturnsId) {
        return null;
    }

    private OrderSync traceUpdate(String traceId) {
        //t_order_trace
        OrderTraceVO orderTraceVO = JdbcTemplateUtils.builder(ModelConst.T_ORDER_TRACE)
                .selectField("*").eq("id", traceId).queryForPOJO(OrderTraceVO.class);
        Assert.notNull(orderTraceVO,"根据发货记录ID未查询到发货记录数据");

//        return OrderSync.builder()
//                .orderTraceList(Collections.singletonList(orderTraceVO))
//                .build();

        return null;
    }

    private OrderSync orderCancel(List<String> orderCustomerIds) {

        //todo 类似  orderCreate

        return null;
    }

    private OrderSync orderTaking(List<String> orderVendorIds) {
        return null;
    }

    private OrderSync saveTransport(List<String> traceIds) {
        return null;
    }

    private OrderSync orderCreate(String orderCustomerId) {
        //
        Assert.isTrue(!StringUtils.isBlank(orderCustomerId),"查询条件不能为空！！！");

        OrderCustomerVO orderCustomerVO = JdbcTemplateUtils.builder(ModelConst.T_ORDER_CUSTOMER)
                .selectField("*").eq("id", orderCustomerId).queryForPOJO(OrderCustomerVO.class);
        Assert.notNull(orderCustomerVO,"根据客户订单ID未查询到客户订单数据");

        List<OrderVendorVO> orderVendorVOList = JdbcTemplateUtils.builder(ModelConst.T_ORDER_VENDOR)
                .selectField("*").eq("order_customer_id", orderCustomerId).queryForList(OrderVendorVO.class);
        Assert.isTrue(!CollectionUtils.isEmpty(orderVendorVOList) && !orderVendorVOList.stream().anyMatch(t -> null == t.getId())
                ,"根据客户订单ID未查询到供应商订单数据");

        List<String> orderVendorIds = orderVendorVOList.stream().map(t -> t.getId()).collect(Collectors.toList());
        List<OrderCustomerGoodsVO> orderCustomerGoodsVOList = JdbcTemplateUtils.builder(ModelConst.T_ORDER_CUSTOMER_GOODS)
                .selectField("*").in("order_vendor_id", orderVendorIds).queryForList(OrderCustomerGoodsVO.class);
        Assert.isTrue(!CollectionUtils.isEmpty(orderCustomerGoodsVOList) && !orderCustomerGoodsVOList.stream().anyMatch(t -> null == t.getId())
                ,"根据供应商订单ID未查询到订单商品数据");


        OrderCustomerExtendsAttr extendsAttr = OrderCustomerExtendsAttr.builder().build();
        if (RegexUtils.notNull(orderCustomerVO.getExtends_attr())) {
            extendsAttr = OrderCustomerExtendsAttr.conversion(orderCustomerVO.getExtends_attr(), false);
        }

        Map<String,Object> orderCustomer = new HashMap<>();
        List<Map<String,Object>> orderVendorList = new ArrayList<>();
        List<Map<String,Object>> orderCustomerGoodsList = new ArrayList<>();

        // todo 主要是状态转换的问题,待确认

//        return OrderSync.builder()
//                .orderCustomerList(orderCustomerVOList)
//                .orderVendorList(orderVendorVOList)
//                .orderCustomerGoodsList(orderCustomerGoodsVOList)
//                .build();

        return null;
    }


}
